package dawn.jxufe.community.entity;

/**
 * @author dawn21
 * @version 1.0
 * @date 2022/7/6 17:48
 * @component 封装分页相关的信息。
 */
public class Page {
    // 当前页码
    private int current = 1;

    // 显示分页按钮上限，10个。
    private int limit = 10;

    // 数据总数(用于计算总页面数)
    private int rows;

    // 查询路径(用于复用分页链接)，用于返回主页面。
    private String path;

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        if (current >= 1){
            this.current = current;
        }
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        if (limit >= 1 && limit <= 100) {
            this.limit = limit;
        }
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        if (rows >= 0) {
            this.rows = rows;
        }
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    //获取当前页的起始行(根据当前页的页码计算)
    public int getOffset(){
        // current * limit -limit
        return (current - 1) * limit;
    }

    //获取总的页数
    public int getTotal(){
        if (rows % limit == 0){
            return rows / limit;
        }else {
            return rows / limit + 1;
        }
    }

    //显示从第几页到第几页
    public int getFrom(){
        int from = current - 2;
        return from < 1 ? 1 : from;
    }

    public int getTo(){
        int to = current + 2;
        int total = getTotal();
        return to > total ? total : to;
    }

}
